﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESpace;

namespace ESpace_Project
{
    public partial class FormHist : Form
    {
       
        History<User> hist;

        User u = new User();

        public FormHist()
        {
            InitializeComponent();
            hist = new History<User>(100, u.Clone(), tBtnUndo, tBtnRedo,"Начало");
            textBox2.Text = u.name + " >> " + u.age.ToString();
            hist.UndoRedoJamp += new History<User>.HistEventDelegate(hist_Jamp);
            ReFillData();
        }
        
        void hist_Jamp(object Sender, User actU, String actComment)
        {
            u = actU.Clone();
            ReFillData();
        }

        void ReFillData()
        {
            textBox2.Text = u.name + " >> " + u.age.ToString();
            txbName.Text = u.name;
            txbAge.Text = u.age.ToString();
        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            u.name = txbName.Text;
            hist.RecHist(u.Clone(), "Смена имени");
            ReFillData();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            u.age = Convert.ToInt32(txbAge.Text);
            hist.RecHist(u.Clone(),"Смена возраста");
            ReFillData();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            hist.Clear(u.Clone(),"Начало");
        }

        private void mBtnUndo_ButtonClick(object sender, EventArgs e)
        {
            hist.Undo();
        }
        
        private void tBtnRedo_ButtonClick(object sender, EventArgs e)
        {
            hist.Redo();
        }

    }

    class User
    {
        public string name;
        public int age;

        public User(String Name, int Age)
        {
            name = Name;
            age = Age;
        }

        public User()
        {
            name = GetRndName();
            age = rnd.Next(20) + 10;
        }

        static Random rnd = new Random();
        String GetRndName()
        {
            string ret = "";
            int l = rnd.Next(5) + 3;
            for (int i = 0; i < l; i++)
                ret += ((char)(rnd.Next(26) + 65)).ToString();
            return ret;
        }

        public User Clone()
        {
            return new User(name, age);
        }

    }
}


